package Factory.Entities;

import Model.Core.ICommunicatorEntity;
import Model.Core.Impl.LinkEntity;
import Model.Core.Impl.MessageEntity;
import Model.Draw.Entities.LinkDraw;
import Model.Draw.Entities.MessageDraw;
import Model.Draw.Events.ManifestEntityDoneListener;
import Model.Draw.Events.ManifestElementEvent;
import Model.Draw.ICommunicatorDraw;
import java.util.ArrayList;
import java.util.List;

/**
 * Projeto de TCC: Editor de Diagramas de comunicação BlueJ
 * @author Jose
 */
public class MessageFactory {
    private List<ManifestEntityDoneListener> listeners;
    
    public MessageFactory(){
        listeners = new ArrayList<ManifestEntityDoneListener>();
    }
    
    public void addListener(ManifestEntityDoneListener listener){
        if(!listeners.contains(listener))
            this.listeners.add(listener);
    }
    
    public void removeListener(ManifestEntityDoneListener listener){
        this.listeners.remove(listener);
    }
    
    public void createEntityGraphicalManifest(MessageEntity messageEntity, Object link, Object target){
        LinkDraw linkDraw = (LinkDraw)link;
        ICommunicatorDraw targetDraw = (ICommunicatorDraw)target;
        MessageDraw graphicalManifest = new MessageDraw(linkDraw, targetDraw);
        messageEntity.setParentLink((LinkEntity)linkDraw.getEntity());
        messageEntity.setTargetEntity((ICommunicatorEntity)targetDraw.getEntity());
        graphicalManifest.setEntity(messageEntity);
        notifyListeners(graphicalManifest);
    }

    private void notifyListeners(MessageDraw graphicalManifest) {
        for(ManifestEntityDoneListener listener : listeners){
            listener.manifestEntityDoneHandler(new ManifestElementEvent(graphicalManifest));
        }
    }    
}
